package com.MarkToTop.util;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.*;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;

/**
 * @CreateDate: Created in 2022-06-22 11:25
 * @Author: hbian
 * @Description:   游戏中的音效类
 */
public class MusicUtil {

	private static AudioClip fly;
	private static Player crash;
	public static void load() {
		try{
			//mp3
			InputStream in = new FileInputStream("music/crash.mp3");
			BufferedInputStream buffer = new BufferedInputStream(in);
			crash = new Player(buffer);
			//wav
			fly = Applet.newAudioClip(new File("music/fly.wav").toURL());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void BGMusic() throws Exception{

		AudioPlayer MGP = AudioPlayer.player;
		AudioStream BGM;
		AudioData MD;
		ContinuousAudioDataStream loop = null;
		try {
			InputStream test = new FileInputStream("music/bg.wav");
			BGM = new AudioStream(test);
			AudioPlayer.player.start(BGM);
			MD = BGM.getData();
			loop = new ContinuousAudioDataStream(MD);
		}
		catch(IOException error) {
			System.out.print(error.toString());
		}
		MGP.start(loop);
	}

	//mp3
	public static void playCrash() {
		try {
			crash.play();
			crash.close();
		}catch(JavaLayerException e){
			e.printStackTrace();
		}
	}
	//wav
	public static void playFly() {
		fly.play();//播放音频

	}
	
	//重置加载mp3资源
	public static void reset() {
		try {
			InputStream in = new FileInputStream("music/crash.mp3");
			BufferedInputStream buffer =new BufferedInputStream(in);
			crash  = new Player(buffer);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}
